﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using KLadderProposal.Interfaces;
using KLadderProposal.Interfaces.BasicStructures;

namespace KLadderProposal.Documents
{
    public class GraphDocument : IGraphDocument
    {
        public void Load(Interfaces.IGraph graph, string filePath)
        {
            int edgesCount = 0;
            string fileContent = File.ReadAllText(filePath);

            string[] pairs = fileContent.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            for (int i = 1; i < pairs.Length; i++)
            {
                string[] labels = pairs[i].Split('\t');
                if (labels.Length > 1)
                {
                    graph.AddUndirectedEdge(labels[0], labels[1].Replace("\r", string.Empty));
                    edgesCount++;
                }
            }

            int maxNNeighborsCount = graph.Nodes.Max(node => node.Neighbors.Count);
            double avgNeighborsCount = graph.Nodes.Average(node => node.Neighbors.Count);
            float median = graph.Nodes.ElementAt(graph.Count / 2).Neighbors.Count +
                graph.Nodes.ElementAt((graph.Count - 1) / 2).Neighbors.Count;
            median /= 2;

            stopwatch.Stop();

            //string reportfilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
            //                                    "JaccardDegreesStatistics.csv");

            //StringBuilder report = new StringBuilder();
            //report.AppendLine("Protein, Neigbors count");
            //foreach (GraphNode node in graph.Nodes)
            //{
            //    report.AppendLine(string.Format("{0},{1}", node.Label, node.Neighbors.Count));
            //}
            //File.AppendAllText(reportfilePath, report.ToString());
        }

        public void Save(Interfaces.IGraph graph, string filePath)
        {
            StringBuilder pajekFileContent = new StringBuilder("*Network KLadder\n\r");

            pajekFileContent.AppendFormat("*Vertices {0}\n\r", graph.Count);
            foreach (GraphNode graphNode in graph.Nodes)
                pajekFileContent.AppendFormat("{0} \"{1}\" 0.0 0.0\n\r", graphNode.SequenceNumber, graphNode.Label);

            pajekFileContent.AppendFormat("*Edges {0}\n\r", graph.Count);
            foreach (GraphNode graphNode in graph.Nodes)
                foreach (GraphNode neighbor in graphNode.Neighbors.Values)
                    pajekFileContent.AppendFormat("{0} {1}\n\r", graphNode.SequenceNumber, neighbor.SequenceNumber);

            pajekFileContent.Append("*Partition KLadder\n\r");
            pajekFileContent.AppendFormat("*Vertices {0}\n\r", graph.Count);
            foreach (GraphNode graphNode in graph.Nodes)
                pajekFileContent.AppendFormat("{0}\n\r", graphNode.ClusterNumber);

            File.WriteAllText(filePath, pajekFileContent.ToString());
        }
    }
}
